package com.crazyimp.utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import lt.startup.duobes.Constants;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.impl.client.DefaultHttpClient;

import android.hardware.Camera.Size;
import android.util.Log;

public class ApiCalls {

	public static String postData(String action, List<NameValuePair> data) {
		try {
			
			URI uri = URIUtils.createURI("http", "www.duobes.lt", -1, "/duobesapi/" + action, null, null);
			String action2 = "";
			Log.d("URI", uri.toString());
			if (data != null) {
				for(int i = 0; i < data.size(); i++){
					action2 += data.get(i).getName() + "="+data.get(i).getValue();
					if(i <= data.size()-1){
						action2 += "&";
					}
				}
//				httpGet.setEntity(new UrlEncodedFormEntity(data));
			}
			Log.e(Constants.LOG, "Api call:"+uri+"?"+action2);
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(uri+"?"+action2);
//			HttpPost httpPost = new HttpPost(uri);
			HttpResponse response;
			response = httpclient.execute(httpGet);
			
			StringBuilder clients = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			String line = reader.readLine();
			while (line != null) {
				clients.append(line);
				line = reader.readLine();
			}
			
			return clients.toString();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (URISyntaxException e) {
			e.printStackTrace();
			return null;
		}
		
	}


}
